package com.xmut.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.LongPKService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.xmut.dao.TopicPraiseDao;
import com.xmut.entity.Topic;
import com.xmut.entity.TopicPraise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TopicPraiseService extends LongPKService<TopicPraise> {

    @Autowired
    private TopicPraiseDao dao;
    @Autowired
    private TopicService topicService;

    @Override
    protected BaseDao<TopicPraise, Long> getDao() {
        return dao;
    }

    /**
     * 获取点赞记录
     * @param userId
     * @param topicId
     * @return
     */
    public TopicPraise getByUserIdWithTopicId(String userId, Long topicId){
        return dao.getByUserIdWithTopicId(userId, topicId);
    }


    public void savePraise(Long topicId) {
        TopicPraise obj = getByUserIdWithTopicId(SysUserUtils.getUserId(), topicId);
        if (obj == null) {
            TopicPraise topicPraise = new TopicPraise();
            topicPraise.setUserId(SysUserUtils.getUserId());
            topicPraise.setTopicId(topicId);
            save(topicPraise);

            Topic topic = topicService.getById(topicId);
            topic.setPraiseNumber(topic.getPraiseNumber() == null ? 1 : topic.getPraiseNumber() + 1 );
            topicService.update(topic);
        }
    }
}
